package com.projectx.action.chat;

import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.flask.colorpicker.OnColorSelectedListener;
import com.flask.colorpicker.builder.ColorPickerClickListener;
import com.flask.colorpicker.builder.ColorPickerDialogBuilder;
import com.google.android.material.textfield.TextInputLayout;
import com.projectx.R;
import com.projectx.action.SettingActivity;
import com.projectx.util.ActivityManage;

import java.time.Duration;

public class ChatSettingActivity extends AppCompatActivity {
    private View myColor;
    private View otherColor;
    private Button commit;
    private EditText editText;//获取输入
    private TextInputLayout textInputLayout;
    private SharedPreferences sharedPreferences;
    private boolean isSaved = true;
    private GradientDrawable myGradientDrawable;
    private GradientDrawable otherGradientDrawable;
    private int tempMyColor;
    private int tempOtherColor;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManage.addActivity("ChatSettingActivity",this);
        setContentView(R.layout.activity_chat_setting);
        try {
            getSupportActionBar().hide(); //去除标题栏
        } catch (NullPointerException e){
            Log.d("KMH","没有状态栏标题");
        }

        sharedPreferences = getSharedPreferences("chat_setting",MODE_PRIVATE);
        myColor = findViewById(R.id.myColor);
        otherColor = findViewById(R.id.otherColor);
        commit = findViewById(R.id.chat_setting_commit);
        myGradientDrawable = (GradientDrawable) myColor.getBackground().mutate();
        otherGradientDrawable = (GradientDrawable) otherColor.getBackground().mutate();

        tempMyColor = sharedPreferences.getInt("myColor",Color.BLACK);
        tempOtherColor = sharedPreferences.getInt("otherColor",Color.BLACK);
        myGradientDrawable.setStroke(10,tempMyColor);
        otherGradientDrawable.setStroke(10,tempOtherColor);

        myColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorPickerDialogBuilder.with(ChatSettingActivity.this).setTitle("选择自己的气泡颜色").initialColor(tempMyColor)
                        .setPositiveButton("确定", new ColorPickerClickListener() {
                            @Override
                            public void onClick(DialogInterface d, int lastSelectedColor, Integer[] allColors) {
                                myGradientDrawable.setStroke(10,lastSelectedColor);
                                isSaved = false;
                                tempMyColor = lastSelectedColor;
                            }
                        })
                        .setNegativeButton("取消",null).build().show();
            }
        });

        otherColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorPickerDialogBuilder.with(ChatSettingActivity.this).setTitle("选择他人的气泡颜色").initialColor(tempOtherColor)
                        .setPositiveButton("确定", new ColorPickerClickListener() {
                            @Override
                            public void onClick(DialogInterface d, int lastSelectedColor, Integer[] allColors) {
                                otherGradientDrawable.setStroke(10,lastSelectedColor);
                                isSaved = false;
                                tempOtherColor = lastSelectedColor;
                            }
                        })
                        .setNegativeButton("取消",null).build().show();
            }
        });

        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("myColor",tempMyColor);
                editor.putInt("otherColor",tempOtherColor);
                editor.apply();
                isSaved = true;
                Toast.makeText(ChatSettingActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onBackPressed() {
        if (isSaved){
            ActivityManage.exitActivity("ChatSettingActivity");
        } else {
            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("警告");
            alert.setMessage("尚未保存是否确定退出");
            alert.setNegativeButton("取消",null);
            alert.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityManage.exitActivity("ChatSettingActivity");
                }
            });
            alert.show();
        }

    }
}
